from django.shortcuts import render,redirect,render_to_response
from django.db.utils import IntegrityError
from django.http import response

from bk_management.models import *

import hashlib
# Create your views here.

#hash加密
def hashPwd(pwd):
    mb5=hashlib.md5()
    mb5.update(pwd.encode())
    result = mb5.hexdigest()
    return result

def loginVaild(fun):
    def inner(request,*args,**kwargs):
        u_name=request.COOKIES.get('u_name')
        id = request.COOKIES.get('id')
        user = User.objects.filter(u_name=u_name,id=id).first()
        if user:
            return fun(request,*args,**kwargs)
        else:
            return redirect('/bk/login/')
    return inner


#登录界面
'''
args:request
return:request,/bk/index,bk_management/login.html
'''
def login(request):
    u_name=request.GET.get("u_name")
    u_pwd=request.GET.get("u_pwd")
    if u_name:
        user = User.objects.filter(u_name=u_name).first()
        u_pwd=hashPwd(u_pwd)
        if user and u_pwd == user.u_pwd:
            response = redirect('/bk/index/')
            response.set_cookie('u_name',u_name)
            response.set_cookie('id',user.id)
            return response
    return render(request,'bk_management/login.html')

#注册界面
'''
注册界面
args:request
return:request,bk_management/register.html
'''
def register(request):
    if request.method == 'POST':
        u_name=request.POST.get('username')
        u_pwd = request.POST.get('password')
        u_pwdre = request.POST.get('re_password')

        empty_flag = u_name and u_pwd and u_pwdre
        same_pwd = u_pwd==u_pwdre

        if empty_flag and same_pwd:

            new_user=User()
            new_user.u_name = u_name
            new_user.u_pwd = hashPwd(u_pwd)
            try:
                new_user.save()
            except Exception:
                error = '重复'
                return render(request,'bk_management/register.html',locals())
            else:
                return redirect('/bk/login/')
    return render(request,'/bk/login/')

#退出登录
def exit(request):
    response = redirect('/bk/login/')
    response.delete_cookie('id')
    response.delete_cookie('u_name')
    return response

@loginVaild
#主界面
def index(request):
    return render(request,'bk_management/index.html')

#商品展示
def show_gtype(request):
    gts=goods_types.objects.all()
    return render(request,'bk_management/goodstype.html',locals())
#商品类型添加
def add_gtype(request):
    if request.method == 'POST':
        gt_name=request.POST.get('gt_name')
        gt_picture=request.FILES.get('gt_picture')
        new_gt = goods_types()
        new_gt.gt_name=gt_name
        new_gt.gt_picture=gt_picture
        try:
            new_gt.save()
        except Exception :
            return redirect('/bk/show_gtype/')
    return redirect('/bk/show_gtype/')

#商品类型修改
def edit_gtype(request):
    if request.method=='POST':
        id=request.POST.get('id')
        gt=goods_types.objects.filter(id =id).first()
        gt_name = request.POST.get('gt_name')
        gt_picture = request.FILES.get('gt_picture')

        if gt_name and gt_picture:
            gt.gt_name = gt_name
            gt.gt_picture=gt_picture
            gt.save()


    return redirect('/bk/show_gtype/')

#商品类型删除
def del_gtype(request,id):
    gt=goods_types.objects.filter(id=id).first()
    if gt:
        gt.delete()
    return redirect('/bk/show_gtype/')